package user.factory;
import book.Library;
import user.AdminUser;
import user.NormalUser;
import user.User;
import utils.PermissionException;
import constant.Constant;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘国水
 * Date: 2025-03-30
 * Time: 18:18
 */
public class ProxyUser {
    private User realUser;
    private Library library;


    public ProxyUser(User user) {
        this.realUser = user;
        library = Library.getLibrary();
    }

    public User getRealUser() {
        return realUser;
    }
    //其他操作
    //调⽤菜单
    public int display() {
        return this.realUser.display();
    }

    //--------------------------------管理员相关⽅法-----------------------------//

    private void checkRealUserWhetherAdminUser(String msg) {
        if(!(this.realUser instanceof AdminUser)) {
            throw new PermissionException(msg);
        }
    }
    //添加书籍操作
    public void addBook() {
        System.out.println("代理类addBook方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限添加图书");
        } catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser)(this.realUser)).addBook();
    }

    //更新书籍操作
    public void updateBook() {
        System.out.println("代理类的updateBook方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限添加图书");
        } catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser)(this.realUser)).updateBook();

    }

    //移除图书
    public void removeBook() {
        System.out.println("代理类的removeBook方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通⽤⼾没有权限删除图书");
        } catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser) realUser).removeBook();

    }

    //查看图书的借阅次数
    public void borrowCount( ) {
        try {
            checkRealUserWhetherAdminUser("普通⽤⼾没有权限查看图书的借阅次数");
        } catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser) realUser).borrowCount();
    }

    //查看最受欢迎的前K本书
    public void generateBook() {
        try {
            checkRealUserWhetherAdminUser("普通⽤⼾没有权限查看最受欢迎的前k本书");
        } catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser) realUser).generateBook();

    }

    //查看库存状态
    public void checkInventoryStatus() {
        try {
            checkRealUserWhetherAdminUser("普通⽤⼾没有权限查看库存状态");
        } catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser) realUser).checkInventoryStatus();
    }

    //移除上架超过1年的书籍
    public void checkAndRemoveOldBooks() {
        try {
            checkRealUserWhetherAdminUser("普通⽤⼾没有权限移除上架超过⼀年的图书");
        } catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser) realUser).checkAndRemoveOldBooks();

    }

    //--------------------------------普通相关⽅法-----------------------------//

    private void checkRealUserWhetherNormalUser(String exceptionMessage) {
        if(!(realUser instanceof NormalUser)){
            throw new PermissionException(exceptionMessage);
        }
    }
    //借阅图书
    public void borrowBook() {
        System.out.println("方法执行了");

        try {
            checkRealUserWhetherNormalUser("管理员请以普通⽤⼾的⽅式借阅图书");
        } catch (PermissionException e) {
            e.printStackTrace();
        }

        ((NormalUser) realUser).borrowBook();

    }

    //归还图书
    public void returnBook() {
        System.out.println("方法执行了");

    }

    //查看个⼈借阅情况
    public void viewBorrowHistory() {

    }

    public void handleOperation(int choice) {
        if(this.realUser instanceof AdminUser) {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBook();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowBook();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBooks();
                    break;
                default:
                    break;
            }
        } else {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBook();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.BORROWED_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowHistory();
                    break;

            }

        }

    }
}
